// App Root
var appPath = getAppPath();

// 是否延迟加载
var lazy = false;

// Datagrid ID
var datagrid_id = "#historyGrid";

// Datagrid URL参数
var datagrid_url = appPath+'/acqu/history_list.do';

/**
 * jQuery加载事件
 */
$(function(){
	// 初始化Datagrid
	initPlugin();
});

/**
 * 控件初始化方法
 */
function initPlugin() {
	$(datagrid_id).datagrid({
		url: lazy? '' : datagrid_url,
		height: "full",
		striped: true,
		remoteSort:false,
		queryParams: getQueryParam(),
		idField:'historyId',
		checkbox:true,
		frozenColumns:[[
		      {field:'ck',checkbox:true}		     
		]],
		columns:[[
		      {field:'title',title:'标题',width:260,align:'left'},
		      {field:'acqName',title:'采集名称',width:130,align:'left'},
		      {field:'categoryName',title:'所属栏目',width:110,align:'left'},
		      {field:'channelUrl',title:'采集地址',width:80,align:'left',
		    	  formatter:function(value,rowData){
		    		  return '<a href="' + value + '">点击浏览</a>';
		    	  }
		      },
		      {field:'contentUrl',title:'内容地址',width:80,align:'left',
		    	  formatter:function(value,rowData){
		    		  return '<a href="' + value + '">点击浏览</a>';
		    	  }
		      },
		      {field:'status',title:'状态',width:50,align:'left',
		    	  formatter:function(value,rowData){
		    		  if (rowData.description == success) {
		    			  return "<span style=\"color:green\">" + msgResource[success] + "</span>";
		    		  } else {
		    			  return "<span style=\"color:red\">" + msgResource[failure] + "</span>";
		    		  }
		    	  }
		      },
		      {field:'description',title:'描述',width:170,align:'left',
		    	  formatter:function(value,rowData){
		    		  return msgResource[value];
		    	  }
		      },
		      {field:'createTime',title:'采集时间',width:130,align:'left',
		    	  formatter:function(value,rowData){
		    		  return $.DateUtil.format(new Date(value),'yyyy/MM/dd hh:mm:ss');
		    	  }
		      }
		 ]],
		 pagination:true,
		 rownumbers:true,
		 onLoadSuccess: function() {
			 uncheckAll(this);
			 changeTipInfo("tommy-cell");
		 },
		 toolbar:[{
				id:'btndelMu',
				text:'删除',
				iconCls:'icon-cancel',
				handler:function(){
					var selections = $(datagrid_id).datagrid('getSelections');
					if(selections == null || selections == ''){
						$.messager.alert('提示信息','请选择要删除的数据','info');
						return ;
					}else{
						$.messager.confirm('提示信息','您确定要删除这些记录？',function(r){
							if(r){
								var ids = '';
								for(var i=0;i<selections.length;i++){
									ids += selections[i].historyId+',';
								}
								ids = ids.substring(0,ids.lastIndexOf(','));
								
								$.loading(true);
								$.ajax({
									url:appPath+'/acqu/history_delMulti.do',
									data:{multi_id:ids},
									success:function(data){
										$.loading(false);
										if(data!='ok'){
											$.messager.alert('提示信息','抱歉，删除未能完成，'+data,'error');
										}else{
											doReload('delete');
										}
									},
									error:function(XMLHttpRequest, textStatus, errorThrown){
										$.loading(false);
										$.messager.alert('提示信息','抱歉，删除未能完成，'+textStatus,'error');
									}
								});		
							}
						});
					}
				}
			}]
	});
	if (lazy)
		$(datagrid_id).datagrid('options').url = datagrid_url;
}

/**
 * 获取请求信息
 * @return {}
 */
function getQueryParam(){
	var submitObject = new Object();	
	return submitObject;
}

/**
 * 重载grid，取消全选
 */
function doReload(type){
	var ops = $(datagrid_id).datagrid('getPager').pagination("options");
	if(type == "save"){
		var pn = Math.floor(ops.total/ops.pageSize)+1;
		$(datagrid_id).datagrid('getPager').pagination({pageNumber:pn});
		$(datagrid_id).datagrid('options').pageNumber = pn;
	}else if(type == "delete"){
		if($(datagrid_id).datagrid('getRows').length == $(datagrid_id).datagrid('getSelections').length){
			var pn ;
			var pnt = Math.ceil(ops.total/ops.pageSize);
			var pm = ops.pageNumber;
			if(pnt == (pm-1)){
				pn = pnt ;
			}else{
				pn = pm
			}
			$(datagrid_id).datagrid('getPager').pagination({pageNumber:(pn==0?1:pn)});
			$(datagrid_id).datagrid('options').pageNumber = (pn==0?1:pn);
		}
	}
	$(datagrid_id).datagrid('unselectAll');
	$(datagrid_id).datagrid('reload');
}
